package cn.wolfcode.web.interceptor;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.service.impl.PermissionServiceImpl;
import cn.wolfcode.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class LoginInterceptor implements HandlerInterceptor {
    @Autowired
    private PermissionServiceImpl permissionService ;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {

        HttpSession session = request.getSession();
        Employee employee = UserContext.getCurrentUser();
        if(employee!=null&&employee.isStatus()){
            if(!employee.isAdmin()){ //如果不是管理员,就需要查询权限信息
                List<String> permissions = permissionService.selectByEmpId(employee.getId());
                session.setAttribute("EXPRESSION_IN_SESSION", permissions);
            }
            return true ;
        }
        response.sendRedirect("/login.html");
        return false;
    }
}
